package com.sifang.bussiness.manager.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sifang.base.util.MchHttpUtil;
import com.sifang.bussiness.dao.order.PayDao;
import com.sifang.bussiness.dao.table.order.MpPay;
import com.sifang.bussiness.dao.withdrawal.WithdrawalDao;
import com.sifang.bussiness.manager.bean.ManagerReqNotifyPay;
import com.sifang.bussiness.manager.bean.ManagerRspNotifyPay;
import com.sifang.bussiness.manager.service.ManagerNotifyPayService;
import com.sifang.bussiness.merchant.service.MerchantNotifyService;


@Service("ManagerNotifyPayService")
public class ManagerNotifyPayServiceImpl implements ManagerNotifyPayService {
	
	private static Logger log = LoggerFactory.getLogger(ManagerNotifyPayServiceImpl.class);
	
	@Autowired
	PayDao payDao;
	
	@Autowired
	WithdrawalDao withdrawalDao;
	
	@Autowired
	MerchantNotifyService merchantNotifyService;

	@Override
	public ManagerRspNotifyPay notifyMerchantPay(ManagerReqNotifyPay managerReqNotifyPay) {
		log.info("通知商户：{}", managerReqNotifyPay.getCode());
		ManagerRspNotifyPay managerRspNotifyPay = new ManagerRspNotifyPay();
		MpPay mpPay = payDao.getOrderByCode(managerReqNotifyPay.getCode());
		//同步通知商户
		int status = merchantNotifyService.payNotify(mpPay);
		//通知次数加一
		payDao.updateAddMerchantNotifyTimesByCode(mpPay.getCode());
		if(status == MchHttpUtil.SEND_STATUS_SUCCESS) {
			managerRspNotifyPay.setMsg("已通知");
			managerRspNotifyPay.setStatus(ManagerRspNotifyPay.RSP_STATUS_SUCCESS);
			return managerRspNotifyPay;
		}else {
			managerRspNotifyPay.setMsg("通知失败");
			managerRspNotifyPay.setStatus(ManagerRspNotifyPay.RSP_STATUS_FAIL);
			return managerRspNotifyPay;
		}
	}

}
